module.exports = function(e) {
    var t = require("../util"), i = null, a = {};
    return t.ext(e.prototype, {
        addHook: function() {
            return this.isHookInstantiated ? this : (function() {
                var e = this;
                if ("undefined" != typeof wx && wx && "function" == typeof wx.request) {
                    i = wx;
                    var n = {
                        request: function(i) {
                            var a = new Date().getTime();
                            if (i && "object" == typeof i && i[0]) {
                                var n, s, o = i[0], r = t.cutUrlSearch(o.url), c = o.success, l = o.fail, u = o && o.header;
                                u && "object" == typeof u || (u = {});
                                var f = {};
                                if (e.getConfig("enableLinkTrace")) {
                                    var p = u["EagleEye-pAppName"];
                                    if (n = u["EagleEye-TraceID"], s = u["EagleEye-SessionID"], n || (n = e.getTraceId()["EagleEye-TraceID"], 
                                    f["EagleEye-TraceID"] = n), s || (s = e.getPageviewId()["EagleEye-SessionID"], f["EagleEye-SessionID"] = s), 
                                    !p) {
                                        var d = e.getConfig("pid");
                                        f["EagleEye-pAppName"] = d;
                                    }
                                }
                                o.success = function() {
                                    var o = new Date().getTime();
                                    if (t.checkAPI(r, !0)) {
                                        var l = arguments && arguments[0] && arguments[0].statusCode || 200;
                                        e.api({
                                            api: i[0].url,
                                            success: !0,
                                            time: o - a,
                                            code: l,
                                            begin: a,
                                            traceId: n,
                                            pv_id: s
                                        });
                                    }
                                    c && c.apply(e, [].slice.call(arguments));
                                }, o.fail = function() {
                                    var o = new Date().getTime();
                                    if (t.checkAPI(r, !0)) {
                                        var c = "";
                                        arguments && arguments[0] && "object" == typeof arguments[0] && (c = (c = JSON.stringify(arguments[0])).substring(0, 1e3));
                                        var u = arguments && arguments[0] && arguments[0].statusCode || "FAILED";
                                        e.api({
                                            api: i[0].url,
                                            success: !1,
                                            time: o - a,
                                            code: u,
                                            msg: c,
                                            begin: a,
                                            traceId: n,
                                            pv_id: s
                                        });
                                    }
                                    l && l.apply(e, [].slice.call(arguments));
                                }, o.header = t.ext({}, u, f);
                            }
                        }
                    };
                    for (var s in wx) if (n[s]) {
                        var o = s.toString();
                        a[o] = function() {
                            return n[o](arguments), i[o].apply(i, [].slice.call(arguments));
                        };
                    } else a[s] = i[s];
                    wx = a;
                }
            }.call(this), this.isHookInstantiated = !0, this);
        },
        removeHook: function() {
            return this.isHookInstantiated ? (function() {
                "undefined" != typeof wx && wx && i && (wx = i, i = null);
            }.call(this), this.isHookInstantiated = !1, this) : this;
        },
        initHook: function() {
            return this.hasInitHook ? this : (this.getConfig("disableHook") || this.addHook(), 
            this.hasInitHook = !0, this);
        }
    }), e;
};